let alarmRuleId = sessionStorage.getItem("selectAlarmRuleId");
let alarmRuleDataType = sessionStorage.getItem("selectAlarmRuleDataType");
let alarmRuleType = sessionStorage.getItem('selectAlarmRuleType');
let alarmRuleCodeDictName = sessionStorage.getItem('selectAlarmRuleCodeDictName');
let alarmRuleBlueNum = sessionStorage.getItem('selectAlarmRuleBlueNum');
let alarmRuleYellowNum = sessionStorage.getItem('selectAlarmRuleYellowNum');
let alarmRuleRedNum = sessionStorage.getItem('selectAlarmRuleRedNum');
let alarmRuleAlarmType = sessionStorage.getItem('selectAlarmRuleAlarmType');
let alarmRuleAlarmTime = sessionStorage.getItem('selectAlarmRuleAlarmTime');

$(function () {
    $('#alarmRuleId').val(alarmRuleId);
    $('#alarmRuleTypeSelect').val(alarmRuleType);
    $('#alarmRuleCodeDictSelect').val(alarmRuleCodeDictName);
    $('#alarmRuleDataTypeSelect').val(alarmRuleDataType);
    $('#alarmRuleBlueNum').val(alarmRuleBlueNum);
    $('#alarmRuleYellowNum').val(alarmRuleYellowNum);
    $('#alarmRuleRedNum').val(alarmRuleRedNum);
    $('#alarmRuleAlarmTypeSelect').val(alarmRuleAlarmType);
    $('#alarmRuleAlarmTime').val(alarmRuleAlarmTime);

    loadCodeDictData();
});

function loadCodeDictData() {
    let selectType = $('#alarmRuleTypeSelect').val();

    switch (Number(selectType)) {
        case 2011:
            $('#alarmTimeUnit').html('秒');
            break;
        case 2051:
            $('#alarmTimeUnit').html('分');
            break;
        case 2061:
            $('#alarmTimeUnit').html('时');
            break;
        case 2031:
            $('#alarmTimeUnit').html('日');
            break;
    }

    showLoading();

    $.post(serverUrl + ':' + serverPort + serverName + '/data/getInfoByDeviceCodeAndType', {
        deviceCode: sessionStorage.getItem("selectDeviceCode"), type: selectType
    }, function (result) {
        hideLoading();

        let obj = JSON.parse(result);
        let content = '<option value="">请选择参数</option>';

        $.each(obj.data, function (i, item) {
            if (!item.name.startsWith('参数') && !item.name.startsWith('CP_') && item.name.trim().length > 0) {
                if (alarmRuleCodeDictName.trim().length > 0 && alarmRuleCodeDictName === item.name) {
                    content += '<option value="' + item.name + '" selected>' + item.name + '</option>';
                } else {
                    content += '<option value="' + item.name + '">' + item.name + '</option>';
                }
            }
        });

        $('#alarmRuleCodeDictSelect').html(content);
    });
}

function saveData(self) {
    let flag = true;
    let dataType = $('#alarmRuleDataTypeSelect').val();
    let codeDictName = $('#alarmRuleCodeDictSelect').val();
    let blueNum = $('#alarmRuleBlueNum').val();
    let yellowNum = $('#alarmRuleYellowNum').val();
    let redNum = $('#alarmRuleRedNum').val();
    let alarmType = $('#alarmRuleAlarmTypeSelect').val();
    let alarmTime = $('#alarmRuleAlarmTime').val();

    if (codeDictName === '') {
        showMessage('error', '请选择设备参数');
        return;
    }

    if (dataType !== '3') {
        if (blueNum === '' || yellowNum === '' || redNum === '') {
            showMessage('error', '请输入数值');
            return;
        }

        if (Number(blueNum) > 99999 || Number(blueNum) < 0 || Number(yellowNum) > 99999 || Number(yellowNum) < 0 || Number(redNum) > 99999 || Number(redNum) < 0) {
            showMessage('error', '数值不合规');
            return;
        }

        if (alarmTime === '') {
            alarmTime = 0;
        }
    } else {
        if (alarmTime === '' || Number(alarmTime) <= 1) {
            showMessage('error', '请输入有效的报警次数');
            return;
        }

        $('#alarmRuleAlarmTypeSelect').val(2);
        alarmType = 2;
    }

    if (flag) {
        showLoading();

        $(self).prop('disabled', true);

        $.post(serverUrl + ':' + serverPort + serverName + '/alarmRule/update', {
            id: $('#alarmRuleId').val(), dataType: $('#alarmRuleDataTypeSelect').val(), deviceCode: sessionStorage.getItem("selectDeviceCode"), type: $('#alarmRuleTypeSelect').val(), codeDictName: codeDictName, blueNum: blueNum, yellowNum: yellowNum, redNum: redNum, alarmType: alarmType, alarmTime: alarmTime
        }, function (result) {
            hideLoading();

            let obj = JSON.parse(result);

            if (obj.code === 0) {
                // 显示成功消息（3秒后自动关闭）
                showMessage('success', '数据保存成功！两秒后自动返回！');

                setTimeout(function () {
                    // 两秒后自动返回
                    goBack();
                }, 2000);
            } else {
                // 显示错误消息（带标题）
                showMessage('error', obj.msg);

                $(self).prop('disabled', false);
            }
        });
    }
}

function resetForm() {
    $('#alarmRuleId').val('');
    $('#alarmRuleTypeSelect').val(2011);
    $('#alarmRuleCodeDictSelect').val('');
    $('#alarmRuleDataTypeSelect').val(0);
    $('#alarmRuleNum').val('');
    $('#alarmRuleAlarmTypeSelect').val(1);
    $('#alarmRuleAlarmTime').val('');

    // 按钮动画效果
    $('#resetBtn').html('<i class="fas fa-check"></i> 已重置');
    $('#resetBtn').css('background', '#f0f0f0');

    setTimeout(() => {
        $('#resetBtn').html('重置');
        $('#resetBtn').css('background', '#f8f8f8');
    }, 1500);
}